package com.wjk.easy.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wjk.easy.R;
import com.wjk.easy.db.Exam;
import com.wjk.easy.db.ExamData;
import com.wjk.easy.db.Size;

import java.util.Date;
import java.util.regex.Pattern;

public class CreateExamDataDialog extends Dialog {

    /**
     * 上下文对象 *
     */
    Activity context;
    private EditText text_weight,text_remark,text_cardId,text_size_width,text_size_height,text_size_other;
    private View.OnClickListener mClickListener;

    public CreateExamDataDialog(Activity context) {
        super(context);
        this.context = context;
    }

    public CreateExamDataDialog(Activity context, int theme, View.OnClickListener clickListener) {
        super(context, theme);
        this.context = context;
        this.mClickListener = clickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定布局
        this.setContentView(R.layout.dialog_add_exam_data);

        text_cardId = findViewById(R.id.text_cardId);
        text_weight = findViewById(R.id.text_weight);
        text_remark = findViewById(R.id.text_remark);
        text_size_width = findViewById(R.id.text_size_width);
        text_size_height = findViewById(R.id.text_size_height);
        text_size_other = findViewById(R.id.text_size_other);

        /*
         * 获取窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
         * 对象,这样这可以以同样的方式改变这个Activity的属性.
         */
        Window dialogWindow = this.getWindow();

        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 0.7); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.7);
        dialogWindow.setAttributes(p);
        dialogWindow.setBackgroundDrawableResource(R.drawable.bg_dialog);

        // 根据id在布局中找到控件对象
        TextView btn_save = findViewById(R.id.tv_save);
        TextView btn_cancel = findViewById(R.id.tv_cancel);

        // 为按钮绑定点击事件监听器
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String cardId = text_cardId.getText().toString().trim();
                String weight = text_weight.getText().toString();
                String remark = text_remark.getText().toString();
                String sizeWidth = text_size_width.getText().toString();
                String sizeHeight = text_size_height.getText().toString();
                String sizeOther = text_size_other.getText().toString();

                if(cardId.length()<1){
                    Toast.makeText(context,"请先刷卡获取编号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(weight.length()<1){
                    Toast.makeText(context,"体重数据为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(sizeWidth.isEmpty()||sizeHeight.isEmpty()){
                    Toast.makeText(context,"尺寸数据不完整",Toast.LENGTH_SHORT).show();
                    return;
                }

                float widthF = Float.parseFloat(sizeWidth);
                float heightF = Float.parseFloat(sizeHeight);
                float otherF=-1f;
                if(!sizeOther.isEmpty())
                    otherF = Float.parseFloat(sizeOther);

                ExamData data = new ExamData(
                        cardId,
                        CreateExamDataDialog.this.currGroupId,
                        new Date(),
                        remark,
                        CreateExamDataDialog.this.currExamId,
                        Float.parseFloat(weight),
                        widthF,
                        heightF,
                        otherF
                );
                view.setTag(data);
                mClickListener.onClick(view);
                CreateExamDataDialog.this.dismiss();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CreateExamDataDialog.this.dismiss();
            }
        });

       this.setCancelable(false);
        setCardId(cardId);
    }
    private int currExamId,currGroupId;
    private String cardId="";
    public void setExam(int currExamId, int currGroupId) {
        this.currExamId=currExamId;
        this.currGroupId = currGroupId;
    }
    public void setCardId(String cardId){
        if(!cardId.isEmpty()){
            this.cardId = cardId;
            if(text_cardId!=null) {
                text_cardId.setText(cardId);
                text_cardId.setSelection(cardId.length());
            }
        }
    }

    public void setSize(String size){
        if(text_size_width!=null) {
            if(text_size_width.getText().toString().isEmpty()){
                text_size_width.setText(size);
                text_size_width.setSelection(size.length());
            }else if(text_size_height.getText().toString().isEmpty()){
                text_size_height.setText(size);
                text_size_height.setSelection(size.length());
            }else{
                text_size_other.setText(size);
                text_size_other.setSelection(size.length());
            }
        }
    }

    public void setWeight(String weight) {
        text_weight.setText(weight);
        text_weight.setSelection(weight.length());
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if( event.getAction() != KeyEvent.ACTION_UP){
           // Log.i("wjk", "dispatchKeyEvent:"+keyCode);
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                String size = mSizeData.toString();
                if (this.isShowing()) {
                    setSize(size);
                }
                isSizeRecording = false;
                return true;
            }
            if (keyCode == KeyEvent.KEYCODE_SPACE) {
                mSizeData = new StringBuffer();
                Log.i("ddddds", event.toString());
                isSizeRecording = true;
            }
            if (isSizeRecording) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_0:
                        mSizeData.append(0);
                        break;
                    case KeyEvent.KEYCODE_1:
                        mSizeData.append(1);
                        break;
                    case KeyEvent.KEYCODE_2:
                        mSizeData.append(2);
                        break;
                    case KeyEvent.KEYCODE_3:
                        mSizeData.append(3);
                        break;
                    case KeyEvent.KEYCODE_4:
                        mSizeData.append(4);
                        break;
                    case KeyEvent.KEYCODE_5:
                        mSizeData.append(5);
                        break;
                    case KeyEvent.KEYCODE_6:
                        mSizeData.append(6);
                        break;
                    case KeyEvent.KEYCODE_7:
                        mSizeData.append(7);
                        break;
                    case KeyEvent.KEYCODE_8:
                        mSizeData.append(8);
                        break;
                    case KeyEvent.KEYCODE_9:
                        mSizeData.append(9);
                        break;
                    case KeyEvent.KEYCODE_PERIOD:
                        mSizeData.append(".");
                        break;
                }
            }
        }
        return super.dispatchKeyEvent(event);
    }

    boolean isSizeRecording;
    StringBuffer mSizeData = new StringBuffer();


}
